<template>
  <div class="icon-body">
    <el-input
      v-model="name"
      style="position: relative;"
      clearable
      placeholder="请输入图标名称"
      @clear="filterIcons"
      @input="filterIcons"
    >
      <template #suffix>
        <el-icon><Search /></el-icon>
      </template>
    </el-input>
    <div class="icon-list">
      <div v-for="(item, index) in iconList" :key="index" @click="selectedIcon(item)">
        <svg-icon :icon-class="item" style="height: 30px;width: 16px;" />
        <span>{{ item }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import icons from './requireIcons'

// 定义组件名称
defineOptions({
  name: 'IconSelect'
})

// 定义emit
const emit = defineEmits(['selected'])

// 响应式数据
const name = ref('')
const iconList = ref(icons)

// 方法
const filterIcons = () => {
  iconList.value = icons
  if (name.value) {
    iconList.value = iconList.value.filter(item => item.includes(name.value))
  }
}

const selectedIcon = (name) => {
  emit('selected', name)
  document.body.click()
}

const reset = () => {
  name.value = ''
  iconList.value = icons
}

// 如果需要暴露方法给父组件使用
defineExpose({
  reset
})
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.icon-body {
  width: 100%;
  padding: 10px;
  .icon-list {
    height: 200px;
    overflow-y: scroll;
    div {
      height: 30px;
      line-height: 30px;
      margin-bottom: -5px;
      cursor: pointer;
      width: 33%;
      float: left;
    }
    span {
      display: inline-block;
      vertical-align: -0.15em;
      fill: currentColor;
      overflow: hidden;
    }
  }
}
</style>
